package com.eascs.workflow.sys.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.web.sso.User;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.ActOrgRoleUserEntity;
import com.eascs.workflow.common.entity.sys.ActOrgUser;
import com.eascs.workflow.common.entity.sys.SysResources;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.entity.sys.SysRoleResources;
import com.eascs.workflow.common.entity.sys.SysUser;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.OrgUserRoleVo;
import com.eascs.workflow.common.vo.OrgUserVO;
import com.eascs.workflow.sys.dao.SysOrgDao;
import com.eascs.workflow.sys.dao.SysOrgRoleUserDao;
import com.eascs.workflow.sys.dao.SysResourcesDao;
import com.eascs.workflow.sys.dao.SysRoleDao;
import com.eascs.workflow.sys.dao.SysUserDao;
import com.eascs.workflow.sys.service.SysPermissionServiceInlet;

@Service
public class SysPermissionServiceInletImpl implements SysPermissionServiceInlet {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	SysRoleDao sysRoleDao;
	@Autowired
	SysResourcesDao sysResourcesDao;
	@Autowired
	SysUserDao sysUserDao;
	@Autowired
	SysOrgDao sysOrgDao;
	@Autowired
	SysOrgRoleUserDao orgRoleUserDao;

	public ModelResult<List<SysResources>> queryResourcesByUser(SysUser user) {
		ModelResult<List<SysResources>> modelResult = new ModelResult<List<SysResources>>();
		if (user == null) {
			modelResult.withError("SysPermissionServiceImpl.queryResourcesByUser", "参数不能为空");
			return modelResult;
		}
		try {
			ActOrgUser actOrgUser = sysOrgDao.getOrgUserByUserNo(user.getEmployeeNo() + "");
			if (actOrgUser == null) {
				logger.error("获取用户权限失败,组织管理未添加该用户");
				modelResult.withError("SysPermissionServiceImpl.queryResourcesByUser", "获取用户权限失败,组织管理未添加该用户");
				return modelResult;
			}
			List<SysResources> defaultRes = sysResourcesDao.selectDefaultResourcesList();
			List<SysRole> roles = sysRoleDao.selectRolesByUser(user);
			if (roles == null || roles.size() == 0) {
				modelResult.setModel(defaultRes);
				return modelResult;
			}
			List<SysResources> res = sysResourcesDao.selectResourcesByRoles(roles);
			res.addAll(defaultRes);
			modelResult.setModel(res);
			return modelResult;
		} catch (Exception e) {
			logger.error("获取用户权限失败:", e);
			modelResult.withError("SysPermissionServiceImpl.queryResourcesByUser", "获取用户失败:" + e.getMessage());
			return modelResult;
		}

	}

	@Override
	public ModelResult<List<SysUser>> queryByUsernameWithRoles(SysRole role) {
		ModelResult<List<SysUser>> modelResult = new ModelResult<List<SysUser>>();
		if (role == null) {
			modelResult.withError("SysPermissionServiceImpl.queryByUsernameWithRoles", "参数不能为空");
			return modelResult;
		}
		try {
			List<SysUser> users = sysUserDao.queryByUsernameWithRoles(role);
			modelResult.setModel(users);
			return modelResult;
		} catch (Exception e) {
			logger.error("获取用户失败:", e);
			modelResult.withError("SysPermissionServiceImpl.queryByUsernameWithRoles", "获取用户失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<List<SysResources>> selectResourcesList() {
		ModelResult<List<SysResources>> modelResult = new ModelResult<List<SysResources>>();
		try {
			List<SysResources> res = sysResourcesDao.selectResourcesList();
			modelResult.setModel(res);
			return modelResult;
		} catch (Exception e) {
			logger.error("获取资源列表失败:", e);
			modelResult.withError("SysPermissionServiceImpl.selectResourcesList", "获取资源列表失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<SysResources> insertResources(SysResources sysResources) {
		ModelResult<SysResources> modelResult = new ModelResult<SysResources>();
		if (sysResources == null) {
			modelResult.withError("SysPermissionServiceImpl.insertResources", "参数不能为空");
			return modelResult;
		}
		try {
			sysResourcesDao.insertResources(sysResources);
			modelResult.setModel(sysResources);
			return modelResult;
		} catch (Exception e) {
			logger.error("新增资源失败:", e);
			modelResult.withError("SysPermissionServiceImpl.insertResources", "新增资源失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> updateResources(SysResources sysResources) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		if (sysResources == null) {
			modelResult.withError("SysPermissionServiceImpl.updateResources", "参数不能为空");
			return modelResult;
		}
		try {
			int result = sysResourcesDao.updateResources(sysResources);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("修改资源失败:", e);
			modelResult.withError("SysPermissionServiceImpl.updateResources", "修改资源失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> deleteResources(SysResources sysResources) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		if (sysResources.getId() == null) {
			modelResult.withError("SysPermissionServiceImpl.deleteResources", "参数不能为空");
			return modelResult;
		}
		try {
			int result = sysResourcesDao.deleteResources(sysResources);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("删除资源失败:", e);
			modelResult.withError("SysPermissionServiceImpl.deleteResources", "删除资源失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<PageResult<Object[]>> selectRolesPage(SysRole param, int pageSize, int pageIndex) {
		ModelResult<PageResult<Object[]>> modelResult = new ModelResult<PageResult<Object[]>>();
		try {
			PageResult<Object[]> pageResult = sysRoleDao.selectRolesPage(param, pageSize, pageIndex);
			modelResult.setModel(pageResult);
			return modelResult;
		} catch (Exception e) {
			logger.error("获取角色列表失败:", e);
			modelResult.withError("SysPermissionServiceImpl.selectRolesPage", "获取角色列表失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<SysRole> selectRolesByRole(SysRole sysRole) {
		ModelResult<SysRole> modelResult = new ModelResult<SysRole>();
		try {
			SysRole role = sysRoleDao.selectRolesByRole(sysRole);
			modelResult.setModel(role);
			return modelResult;
		} catch (Exception e) {
			logger.error("获取角色失败:", e);
			modelResult.withError("SysPermissionServiceImpl.selectRolesByRole", "获取角色失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> insertRole(SysRole sysRole) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		if (sysRole == null) {
			modelResult.withError("SysPermissionServiceImpl.insertRole", "参数不能为空");
			return modelResult;
		}
		try {
			int result = sysRoleDao.insertRole(sysRole);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("新增角色失败:", e);
			modelResult.withError("SysPermissionServiceImpl.insertRole", "新增角色失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> deleteRole(SysRole sysRole) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		if (sysRole == null) {
			modelResult.withError("SysPermissionServiceImpl.deleteRole", "参数不能为空");
			return modelResult;
		}
		try {
			int result = sysRoleDao.deleteRole(sysRole);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("删除角色失败:", e);
			modelResult.withError("SysPermissionServiceImpl.deleteRole", "删除角色失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> updateRole(SysRole sysRole) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		if (sysRole == null) {
			modelResult.withError("SysPermissionServiceImpl.updateRole", "参数不能为空");
			return modelResult;
		}
		try {
			int result = sysRoleDao.updateRole(sysRole);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("修改角色失败:", e);
			modelResult.withError("SysPermissionServiceImpl.updateRole", "修改角色失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> insertRoleResBatch(List<SysRoleResources> roleResList) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		try {
			String roleId = roleResList.get(0).getRoleId();
			sysRoleDao.deleteRoleRes(roleId);
			int result = sysRoleDao.insertRoleResBatch(roleResList);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("批量：新增角色资源失败:", e);
			modelResult.withError("SysPermissionServiceImpl.insertRoleResBatch", "批量：新增角色资源失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<List<SysResources>> selectResourcesByRoles(SysRole role) {
		ModelResult<List<SysResources>> modelResult = new ModelResult<List<SysResources>>();
		if (role == null) {
			modelResult.withError("SysPermissionServiceImpl.selectResourcesByRoles", "参数不能为空");
			return modelResult;
		}
		try {
			List<SysRole> roles = new ArrayList<>();
			roles.add(role);
			List<SysResources> res = sysResourcesDao.selectResourcesByRoles(roles);
			List<SysResources> defaultRes = sysResourcesDao.selectDefaultResourcesList();
			res.addAll(defaultRes);
			modelResult.setModel(res);
			return modelResult;
		} catch (Exception e) {
			logger.error("获取角色权限失败:", e);
			modelResult.withError("SysPermissionServiceImpl.selectResourcesByRoles", "获取角色失败:" + e.getMessage());
			return modelResult;
		}
	}

	public ModelResult<PageResult<OrgUserRoleVo>> searchOrgUserRolePage(Map<String, String> param, int pageSize, int pageIndex) {
		ModelResult<PageResult<OrgUserRoleVo>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(orgRoleUserDao.searchOrgUserRolePage(param, pageSize, pageIndex));
		} catch (Exception e) {
			logger.error("查询人员角色列表失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.searchUserRolePage", "查询人员角色列表失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<PageResult<User>> searchEmployeePage(Map<String, String> param, int pageSize, int pageIndex) {
		ModelResult<PageResult<User>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysUserDao.searchEmployeePage(param, pageSize, pageIndex));
		} catch (Exception e) {
			logger.error("查询人员角色列表失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.searchUserRolePage", "查询人员角色列表失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getOrgListByLevel(String parentId, int level) {
		ModelResult<List<ActOrgEntity>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getOrgListByLevel(parentId, level));
		} catch (Exception e) {
			logger.error("查询人员角色列表失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.searchUserRolePage", "查询人员角色列表失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<SysRole>> getRoleList() {
		ModelResult<List<SysRole>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysRoleDao.getRoleList());
		} catch (Exception e) {
			logger.error("查询角色列表失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getRoleList", "查询角色列表失败：" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public ModelResult<User> getSysUserById(String userNo) {
		ModelResult<User> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysUserDao.getSysUserById(userNo));
		} catch (Exception e) {
			logger.error("查询员工信息失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getSysUserById", "查询员工信息失败：" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public ModelResult<SysRole> getRoleById(String roleId) {
		ModelResult<SysRole> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysRoleDao.getById(roleId));
		} catch (Exception e) {
			logger.error("查询角色层级失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getSysUserById", "查询角色层级失败：" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	@Transactional("wfTransactionManager")
	public ModelResult<Integer> saveOrUpdateOrgUserRole(ActOrgRoleUserEntity orgRoleUserEntity) {
		ModelResult<Integer> modelResult = new ModelResult<>();
		try {
			if (StringUtil.isNull(orgRoleUserEntity.getId())) {
				// 检查用户权限是否已存在
				Boolean isExist = orgRoleUserDao.checkExist(orgRoleUserEntity.getUserNo(), orgRoleUserEntity.getRoleId(), orgRoleUserEntity.getOrgId());
				if (isExist) {
					logger.error("新增用户角色权限失败,userNo:{},roleId:{},orgId:{}的记录已存在", orgRoleUserEntity.getUserNo(), orgRoleUserEntity.getRoleId(), orgRoleUserEntity.getOrgId());
					modelResult.withError("SysPermissionServiceImpl.saveOrgUserRole", "新增记录失败,用户权限已存在");
				} else {
					modelResult.withModel(orgRoleUserDao.saveOrUpdateOrgUserRole(orgRoleUserEntity));
				}
			} else {
				modelResult.withModel(orgRoleUserDao.saveOrUpdateOrgUserRole(orgRoleUserEntity));
			}
		} catch (Exception e) {
			logger.error("保存用户角色权限失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.saveOrgUserRole", "保存用户角色权限失败:" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<Object[]>> getLikeEmployeeList(String likeStr) {
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysUserDao.getLikeEmployeeList(likeStr));
		} catch (Exception e) {
			logger.error("获取联想用户失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getLikeEmployeeList", "获取联想用户失败:" + e.getMessage());
		}
		return modelResult;
	}

	public ModelResult<Integer> delUserRole(String id) {
		ModelResult<Integer> modelResult = new ModelResult<>();
		try {
			if (StringUtil.isNull(id)) {
				modelResult.withError("SysPermissionServiceImpl.delUserRole", "删除失败, Id不能为空！");
			} else {
				modelResult.withModel(orgRoleUserDao.deleteById(id));
			}
		} catch (Exception e) {
			logger.error("删除用户角色权限失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.delUserRole", "删除用户角色权限失败:" + e.getMessage());
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<ActOrgRoleUserEntity>> getUserRoleList(String userNo) {
		ModelResult<List<ActOrgRoleUserEntity>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(orgRoleUserDao.getUserRoleList(userNo));
		} catch (Exception e) {
			logger.error("查询用户角色列表失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getUserRoleList", "查询用户角色列表失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<Boolean> checkIsAdmin(String userNo) {
		ModelResult<Boolean> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysUserDao.checkIsAdmin(userNo));
		} catch (Exception e) {
			logger.error("验证是否超级管理员失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.checkIsAdmin", "验证是否超级管理员失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<Boolean> checkAuth(String userNo, String roleCode) {
		ModelResult<Boolean> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysUserDao.checkUserRole(userNo, roleCode));
		} catch (Exception e) {
			logger.error("验证是否用户关闭权限失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.checkAuth", "验证是否用户关闭权限失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<Object[]>> getOperatorList(String userName) {
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getOperatorList(userName));
		} catch (Exception e) {
			logger.error("经办人联想查询失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getOperatorList", "经办人联想查询失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<List<Object[]>> getOperatorListByRoleCode(String userName, String roleCode) {
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getOperatorListByRoleCode(userName, roleCode));
		} catch (Exception e) {
			logger.error("经办人联想查询失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getOperatorListByRoleCode", "经办人联想查询失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<PageResult<OrgUserVO>> queryPageActOrgUser(String orgId, int pageSize, int pageIndex) {
		ModelResult<PageResult<OrgUserVO>> modelResult = new ModelResult<>();
		try {
			// 获取该组织的所有的子节点
			List<String> orgIds = getChildrenIds(orgId);
			// 分页查找组织下的员工
			PageResult<ActOrgUser> page = sysOrgDao.queryPageActOrgUser(orgIds, pageSize, pageIndex);
			PageResult<OrgUserVO> result = new PageResult<>(pageSize, page.getPageIndex(), page.getDbRowCount());
			List<ActOrgUser> orgUserList = page.getData();
			List<OrgUserVO> orgUserVOList = new ArrayList<>();
			for (ActOrgUser actOrgUser : orgUserList) {
				ActOrgEntity actOrgEntity = sysOrgDao.getOrgByIds(actOrgUser.getOrgId());
				User employee = sysUserDao.getSysUserById(actOrgUser.getUserNo());
				SysUser user = new SysUser();
				user.setEmployeeNo(Integer.valueOf(actOrgUser.getUserNo()));
				List<SysRole> roles = sysRoleDao.selectRolesByUser(user);
				List<String> roleNameList = new ArrayList<>();
				for (SysRole sysRole : roles) {
					roleNameList.add(sysRole.getRoleName());
				}
				String roleStr = StringUtils.join(roleNameList, ",");
				OrgUserVO orgUserVO = new OrgUserVO();
				BeanUtils.copyProperties(actOrgUser, orgUserVO);
				orgUserVO.setRoles(roleStr);
				orgUserVO.setEmployee(employee);
				orgUserVO.setActOrgEntity(actOrgEntity);
				orgUserVOList.add(orgUserVO);
			}
			result.setData(orgUserVOList);
			modelResult.setModel(result);
		} catch (Exception e) {
			logger.error("获取组织下的所有用户失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.queryPageActOrgUser", "获取组织下的所有用户失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<Integer> insertActOrgUser(ActOrgUser actOrgUser) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		try {
			int result = sysOrgDao.insertActOrgUser(actOrgUser);
			modelResult.setModel(result);
			return modelResult;
		} catch (Exception e) {
			logger.error("新增组织用户关联表失败:", e);
			modelResult.withError("SysPermissionServiceImpl.insertActOrgUser", "新增织用户关联表失败:" + e.getMessage());
			return modelResult;
		}
	}

	@Override
	public ModelResult<Integer> deleteActOrgUser(String id, String userNo) {
		ModelResult<Integer> modelResult = new ModelResult<Integer>();
		try {
			SysUser user = new SysUser();
			user.setEmployeeNo(Integer.valueOf(userNo));
			List<SysRole> roles = sysRoleDao.selectRolesByUser(user);
			if (roles.size() <= 0) {
				int result = sysOrgDao.deleteActOrgUser(id);
				modelResult.setModel(result);
			} else {
				logger.error("删除组织用户关联表失败，该用户拥有角色无法删除");
				modelResult.withError("SysPermissionServiceImpl.deleteActOrgUser", "该用户拥有角色无法删除");
			}
			return modelResult;
		} catch (Exception e) {
			logger.error("删除组织用户关联表失败:", e);
			modelResult.withError("SysPermissionServiceImpl.deleteActOrgUser", "删除组织用户关联表失败:" + e.getMessage());
			return modelResult;
		}
	}

	/** 获取该组织的所有的子节点 */
	private List<String> getChildrenIds(String orgId) {
		List<ActOrgEntity> result = new ArrayList<ActOrgEntity>();
		ActOrgEntity orgEntity = sysOrgDao.getOrgByIds(orgId);
		if (orgEntity != null) {
			result.add(orgEntity);
			List<ActOrgEntity> tmpActOrgList = new ArrayList<ActOrgEntity>();
			tmpActOrgList.add(orgEntity);
			while (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
				List<String> parentIds = getParentIds(tmpActOrgList);
				tmpActOrgList = sysOrgDao.getOrgByParentIds(parentIds);
				if (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
					result.addAll(tmpActOrgList);
				}
			}
		}
		List<String> orgIds = getParentIds(result);
		return orgIds;
	}

	private List<String> getParentIds(List<ActOrgEntity> actOrgList) {
		List<String> parentIds = new ArrayList<String>();
		for (ActOrgEntity org : actOrgList) {
			parentIds.add(org.getOrgId());
		}
		return parentIds;
	}

	@Override
	public ModelResult<List<Object[]>> getOrgListLikeName(String orgName, Integer level) {
		ModelResult<List<Object[]>> modelResult = new ModelResult<>();
		try {
			if (null != level && level > 0) {
				modelResult.withModel(sysOrgDao.getOrgListLikeName(orgName, level));
			} else {
				modelResult.withModel(sysOrgDao.getOrgAllLikeName(orgName));
			}

		} catch (Exception e) {
			logger.error("组织架构模糊搜索失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.getOrgLikeName", "组织架构模糊搜索失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public List<ActOrgEntity> getChildOrgByOrgId(String orgId) {
		ActOrgEntity orgEntity = sysOrgDao.getOrgByIds(orgId);
		return getChildOrg(orgEntity);
	}

	private List<ActOrgEntity> getChildOrg(ActOrgEntity orgEntity) {
		List<ActOrgEntity> result = new ArrayList<ActOrgEntity>();
		if (orgEntity != null) {
			// 获取所有的子节点
			result.add(orgEntity);
			List<ActOrgEntity> tmpActOrgList = new ArrayList<ActOrgEntity>();
			tmpActOrgList.add(orgEntity);
			while (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
				List<String> parentIds = getParentIds(tmpActOrgList);
				tmpActOrgList = sysOrgDao.getOrgByParentIds(parentIds);
				if (tmpActOrgList != null && !tmpActOrgList.isEmpty()) {
					result.addAll(tmpActOrgList);
				}
			}

			// 移除非平台的节点
			Iterator<ActOrgEntity> iterators = result.iterator();
			while (iterators.hasNext()) {
				if (iterators.next().getOrgLevel() != ActOrgEntity.ORG_LEVEL_PLATFORM) {
					iterators.remove();
				}
			}
		}
		return result;
	}

	@Override
	public ModelResult<List<ActOrgEntity>> getLeafNodeByParentId(String orgId) {
		ModelResult<List<ActOrgEntity>> modelResult = new ModelResult<>();
		try {
			modelResult.withModel(sysOrgDao.getLeafNodeByParentId(orgId));
		} catch (Exception e) {
			logger.error("查询人员角色列表失败", e.getMessage());
			modelResult.withError("SysPermissionServiceImpl.searchUserRolePage", "查询人员角色列表失败:" + e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}
}
